Passed
Pull Request — master (#47)
by
unknown
05:23
created

button.js ➔ _interopDefaultLegacy   B

Complexity

Conditions 7

Size

Total Lines 1
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 1
dl 0
loc 1
rs 8
c 0
b 0
f 0
1
/*!
2
  * Bootstrap button.js v4.6.0 (https://getbootstrap.com/)
3
  * Copyright 2011-2021 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
4
  * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
5
  */
6
(function (global, factory) {
7
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('jquery')) :
8
  typeof define === 'function' && define.amd ? define(['jquery'], factory) :
0 ignored issues
show
Bug introduced by
The variable define seems to be never declared. If this is a global, consider adding a /** global: define */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
9
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Button = factory(global.jQuery));
0 ignored issues
show
Bug introduced by
The variable globalThis seems to be never declared. If this is a global, consider adding a /** global: globalThis */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Best Practice introduced by
If you intend to check if the variable self is declared in the current environment, consider using typeof self === "undefined" instead. This is safe if the variable is not actually declared.
Loading history...
Comprehensibility introduced by
Usage of the sequence operator is discouraged, since it may lead to obfuscated code.

The sequence or comma operator allows the inclusion of multiple expressions where only is permitted. The result of the sequence is the value of the last expression.

This operator is most often used in for statements.

Used in another places it can make code hard to read, especially when people do not realize it even exists as a seperate operator.

This check looks for usage of the sequence operator in locations where it is not necessary and could be replaced by a series of expressions or statements.

var a,b,c;

a = 1, b = 1,  c= 3;

could just as well be written as:

var a,b,c;

a = 1;
b = 1;
c = 3;

To learn more about the sequence operator, please refer to the MDN.

Loading history...
10
}(this, (function ($) { 'use strict';
11
12
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
13
14
  var $__default = /*#__PURE__*/_interopDefaultLegacy($);
15
16
  function _defineProperties(target, props) {
17
    for (var i = 0; i < props.length; i++) {
18
      var descriptor = props[i];
19
      descriptor.enumerable = descriptor.enumerable || false;
20
      descriptor.configurable = true;
21
      if ("value" in descriptor) descriptor.writable = true;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
22
      Object.defineProperty(target, descriptor.key, descriptor);
23
    }
24
  }
25
26
  function _createClass(Constructor, protoProps, staticProps) {
27
    if (protoProps) _defineProperties(Constructor.prototype, protoProps);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
28
    if (staticProps) _defineProperties(Constructor, staticProps);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
29
    return Constructor;
30
  }
31
32
  /**
33
   * ------------------------------------------------------------------------
34
   * Constants
35
   * ------------------------------------------------------------------------
36
   */
37
38
  var NAME = 'button';
39
  var VERSION = '4.6.0';
40
  var DATA_KEY = 'bs.button';
41
  var EVENT_KEY = "." + DATA_KEY;
42
  var DATA_API_KEY = '.data-api';
43
  var JQUERY_NO_CONFLICT = $__default['default'].fn[NAME];
44
  var CLASS_NAME_ACTIVE = 'active';
45
  var CLASS_NAME_BUTTON = 'btn';
46
  var CLASS_NAME_FOCUS = 'focus';
47
  var SELECTOR_DATA_TOGGLE_CARROT = '[data-toggle^="button"]';
48
  var SELECTOR_DATA_TOGGLES = '[data-toggle="buttons"]';
49
  var SELECTOR_DATA_TOGGLE = '[data-toggle="button"]';
50
  var SELECTOR_DATA_TOGGLES_BUTTONS = '[data-toggle="buttons"] .btn';
51
  var SELECTOR_INPUT = 'input:not([type="hidden"])';
52
  var SELECTOR_ACTIVE = '.active';
53
  var SELECTOR_BUTTON = '.btn';
54
  var EVENT_CLICK_DATA_API = "click" + EVENT_KEY + DATA_API_KEY;
55
  var EVENT_FOCUS_BLUR_DATA_API = "focus" + EVENT_KEY + DATA_API_KEY + " " + ("blur" + EVENT_KEY + DATA_API_KEY);
56
  var EVENT_LOAD_DATA_API = "load" + EVENT_KEY + DATA_API_KEY;
57
  /**
58
   * ------------------------------------------------------------------------
59
   * Class Definition
60
   * ------------------------------------------------------------------------
61
   */
62
63
  var Button = /*#__PURE__*/function () {
64
    function Button(element) {
65
      this._element = element;
66
      this.shouldAvoidTriggerChange = false;
67
    } // Getters
68
69
70
    var _proto = Button.prototype;
71
72
    // Public
73
    _proto.toggle = function toggle() {
74
      var triggerChangeEvent = true;
75
      var addAriaPressed = true;
76
      var rootElement = $__default['default'](this._element).closest(SELECTOR_DATA_TOGGLES)[0];
77
78
      if (rootElement) {
79
        var input = this._element.querySelector(SELECTOR_INPUT);
80
81
        if (input) {
82
          if (input.type === 'radio') {
83
            if (input.checked && this._element.classList.contains(CLASS_NAME_ACTIVE)) {
84
              triggerChangeEvent = false;
85
            } else {
86
              var activeElement = rootElement.querySelector(SELECTOR_ACTIVE);
87
88
              if (activeElement) {
89
                $__default['default'](activeElement).removeClass(CLASS_NAME_ACTIVE);
90
              }
91
            }
92
          }
93
94
          if (triggerChangeEvent) {
95
            // if it's not a radio button or checkbox don't add a pointless/invalid checked property to the input
96
            if (input.type === 'checkbox' || input.type === 'radio') {
97
              input.checked = !this._element.classList.contains(CLASS_NAME_ACTIVE);
98
            }
99
100
            if (!this.shouldAvoidTriggerChange) {
101
              $__default['default'](input).trigger('change');
102
            }
103
          }
104
105
          input.focus();
106
          addAriaPressed = false;
107
        }
108
      }
109
110
      if (!(this._element.hasAttribute('disabled') || this._element.classList.contains('disabled'))) {
111
        if (addAriaPressed) {
112
          this._element.setAttribute('aria-pressed', !this._element.classList.contains(CLASS_NAME_ACTIVE));
113
        }
114
115
        if (triggerChangeEvent) {
116
          $__default['default'](this._element).toggleClass(CLASS_NAME_ACTIVE);
117
        }
118
      }
119
    };
120
121
    _proto.dispose = function dispose() {
122
      $__default['default'].removeData(this._element, DATA_KEY);
123
      this._element = null;
124
    } // Static
125
    ;
126
127
    Button._jQueryInterface = function _jQueryInterface(config, avoidTriggerChange) {
128
      return this.each(function () {
129
        var $element = $__default['default'](this);
130
        var data = $element.data(DATA_KEY);
131
132
        if (!data) {
133
          data = new Button(this);
134
          $element.data(DATA_KEY, data);
135
        }
136
137
        data.shouldAvoidTriggerChange = avoidTriggerChange;
138
139
        if (config === 'toggle') {
140
          data[config]();
141
        }
142
      });
143
    };
144
145
    _createClass(Button, null, [{
146
      key: "VERSION",
147
      get: function get() {
148
        return VERSION;
149
      }
150
    }]);
151
152
    return Button;
153
  }();
154
  /**
155
   * ------------------------------------------------------------------------
156
   * Data Api implementation
157
   * ------------------------------------------------------------------------
158
   */
159
160
161
  $__default['default'](document).on(EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE_CARROT, function (event) {
162
    var button = event.target;
163
    var initialButton = button;
164
165
    if (!$__default['default'](button).hasClass(CLASS_NAME_BUTTON)) {
166
      button = $__default['default'](button).closest(SELECTOR_BUTTON)[0];
167
    }
168
169
    if (!button || button.hasAttribute('disabled') || button.classList.contains('disabled')) {
170
      event.preventDefault(); // work around Firefox bug #1540995
171
    } else {
172
      var inputBtn = button.querySelector(SELECTOR_INPUT);
173
174
      if (inputBtn && (inputBtn.hasAttribute('disabled') || inputBtn.classList.contains('disabled'))) {
175
        event.preventDefault(); // work around Firefox bug #1540995
176
177
        return;
178
      }
179
180
      if (initialButton.tagName === 'INPUT' || button.tagName !== 'LABEL') {
181
        Button._jQueryInterface.call($__default['default'](button), 'toggle', initialButton.tagName === 'INPUT');
182
      }
183
    }
184
  }).on(EVENT_FOCUS_BLUR_DATA_API, SELECTOR_DATA_TOGGLE_CARROT, function (event) {
185
    var button = $__default['default'](event.target).closest(SELECTOR_BUTTON)[0];
186
    $__default['default'](button).toggleClass(CLASS_NAME_FOCUS, /^focus(in)?$/.test(event.type));
187
  });
188
  $__default['default'](window).on(EVENT_LOAD_DATA_API, function () {
189
    // ensure correct active class is set to match the controls' actual values/states
190
    // find all checkboxes/readio buttons inside data-toggle groups
191
    var buttons = [].slice.call(document.querySelectorAll(SELECTOR_DATA_TOGGLES_BUTTONS));
192
193
    for (var i = 0, len = buttons.length; i < len; i++) {
194
      var button = buttons[i];
195
      var input = button.querySelector(SELECTOR_INPUT);
196
197
      if (input.checked || input.hasAttribute('checked')) {
198
        button.classList.add(CLASS_NAME_ACTIVE);
199
      } else {
200
        button.classList.remove(CLASS_NAME_ACTIVE);
201
      }
202
    } // find all button toggles
203
204
205
    buttons = [].slice.call(document.querySelectorAll(SELECTOR_DATA_TOGGLE));
206
207
    for (var _i = 0, _len = buttons.length; _i < _len; _i++) {
208
      var _button = buttons[_i];
209
210
      if (_button.getAttribute('aria-pressed') === 'true') {
211
        _button.classList.add(CLASS_NAME_ACTIVE);
212
      } else {
213
        _button.classList.remove(CLASS_NAME_ACTIVE);
214
      }
215
    }
216
  });
217
  /**
218
   * ------------------------------------------------------------------------
219
   * jQuery
220
   * ------------------------------------------------------------------------
221
   */
222
223
  $__default['default'].fn[NAME] = Button._jQueryInterface;
224
  $__default['default'].fn[NAME].Constructor = Button;
225
226
  $__default['default'].fn[NAME].noConflict = function () {
227
    $__default['default'].fn[NAME] = JQUERY_NO_CONFLICT;
228
    return Button._jQueryInterface;
229
  };
230
231
  return Button;
232
233
})));
234
//# sourceMappingURL=button.js.map
235